<template>
    <div>
        <el-tree :data="datas" show-checkbox default-expand-all node-key="id" ref="tree" highlight-current
            :props="defaultProps" :default-checked-keys="mids">
        </el-tree>

        <div class="buttons">
            <el-button style="margin: 10px;" plain type="warning" @click="getCheckedKeys">保存更改</el-button>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            datas: null,
            mids: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            }
        };
    },
    model: {
        prop: "dialogFormVisible",
        event: "change",
    },
    props: {
        dialogFormVisible: Boolean,
        data: Object,
    },
    methods: {
        //保持更改
        getCheckedKeys() {
            this.axios({
                method: 'Put',
                url: 'Auth/Upd0rAddAuth?id=' + JSON.parse(JSON.stringify(this.data)).RoleID,
                data: JSON.stringify(this.$refs.tree.getCheckedKeys()),
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(res => {
                if (res.data.code > 0) {
                    this.$notify({
                        title: '成功',
                        message: res.data.msg,
                        type: 'success'
                    });
                    this.$emit('refresh')
                    this.$emit("change", false);
                } else {
                    this.$notify.error({
                        title: '错误',
                        message: res.data.msg
                    });
                }
            }).catch(err => {
                console.log('err: ', err);
            })
        },
        select() {
            //获取菜单
            this.axios({
                method: 'get',
                url: 'Authority'
            }).then(res => {
                this.datas = res.data;
            }).catch(err => {
                console.log('err: ', err);
            })
            //获取该角色的菜单编号
            this.axios({
                method: 'get',
                url: '/Auth/GetMid',
                params: { id: JSON.parse(JSON.stringify(this.data)).RoleID },
            }).then(res => {
                this.mids = res.data;
                this.$refs.tree.setCheckedKeys(this.mids);
            }).catch(err => {
                console.log('err: ', err);
            })


        }
    },
    watch: {
        dialogFormVisible(v) {
            if (v) this.select()
        }
    },
    created() {
        this.select()
    }
};
</script>
